﻿using Company.Application.Share.Entities;
using Company.Application.Share.Interfaces;
using Company.Core.Helpers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Company.Shell.Services
{
    class WebManager : IWebManager
    {
        private readonly string url = "https://localhost:7035";

        public async Task<T> GetDevicesAsync<T>(int hospitalId, int stationId)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var requestUri = url + $"/Device/GetDevices?hospitalId={hospitalId}&stationId={stationId}";
                    var response = await client.GetAsync(requestUri);
                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<T>(json);
                    }
                }
            }
            catch (Exception ex)
            {
                Logs.Error(ex);
            }
            return default(T);
        }

        public async Task<T> GetHospitalsAsync<T>(string keyword)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var requestUri = url + $"/Hospital/GetHospitals?keyword={keyword}";
                    var response = await client.GetAsync(requestUri);
                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<T>(json);
                    }
                }
            }
            catch (Exception ex)
            {
                Logs.Error(ex);
            }
            return default(T);
        }

        public async Task<T> GetStationsAsync<T>(int hospitalId)
        {
            try
            {
                using(HttpClient client = new HttpClient())
                {
                    var requestUri = url + $"/Station/GetStations?hospitalId={hospitalId}";
                    var response = await client.GetAsync(requestUri);
                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<T>(json);
                    }
                }
            }
            catch (Exception ex)
            {
                Logs.Error(ex);
            }
            return default(T);
        }

        public async Task<T> LoginAsync<T>(string username, string password, int hospitalId)
        {
            try
            {
                using(HttpClient client = new HttpClient())
                {
                    var requestUri = url + $"/User/LoginByGet?username={username}&password={password}&hospitalId={hospitalId}";
                    var response = await client.GetAsync(requestUri);
                    if(response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<T>(json);
                    }
                }
            }
            catch(Exception ex)
            {
                Logs.Error(ex);
            }
            return default(T);
        }

        public async Task<T> UpdateAsync<T>(DeviceEntity entity)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var requestUri = url + "/Device/Update";
                    var content = new StringContent(JsonConvert.SerializeObject(entity), Encoding.UTF8, "application/json");
                    var response = await client.PostAsync(requestUri, content);
                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<T>(json);
                    }
                }
            }
            catch (Exception ex)
            {
                Logs.Error(ex);
            }
            return default(T);
        }
    }
}
